<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Box;
use Illuminate\Http\Request;

class CategoryController extends AdminController
{
    public function index(Content $content)
    {
        return $content->title($this->title())
            ->description(trans('admin.list'))
            ->body(function (Row $row) {
               $row->column(8,$this->grid()->render());

               $row->column(4,function (Column $column) {
                  $form = new \Dcat\Admin\Widgets\Form();
                  $form->action(admin_url('categories'));
                  $form->text('title')->required(true);
                  $form->number('sort')->default(99);
                  $column->append(Box::make(trans('admin.new'),$form));
               });
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->column('id');
            $grid->column('title');
            $grid->column('sort');
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->quickSearch(['title']);
            $grid->disableViewButton();
            $grid->disableCreateButton();

            $grid->model()->orderBy('sort', 'asc');

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('sort');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function apiList()
    {
        return \App\Models\Category::query()->selectRaw('id,title as text')->get()->toArray();
    }
}
